package offer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author Elephas
 * @Date 2022/1/30
 **/
@FunctionalInterface
public interface MaxScoreIndices {
    List<Integer> maxScoreIndices(int[] nums);
}
class MaxScoreIndicesImpl1 implements MaxScoreIndices{

    @Override
    public List<Integer> maxScoreIndices(int[] nums) {
        List<Integer> ans = new ArrayList<>();
        int[] dp = new int[nums.length + 1];
        dp[0] = validNumber(nums,0,nums.length - 1, 1);
        dp[nums.length] = validNumber(nums,0,nums.length - 1,0);
        int max = dp[0] > dp[nums.length] ? dp[0] : dp[nums.length];
        for (int i = 1; i < nums.length; i++) {
            dp[i] = validNumber(nums,0,i-1,0) + validNumber(nums,i,nums.length-1,1);
            if(max < dp[i]){
                max = dp[i];
            }
        }
        for (int i = 0; i < dp.length; i++) {
            if(max == dp[i]){
                ans.add(i);
            }
        }
        return ans;
    }
    private int validNumber(int[] nums,int low,int high,int target){
        int sum = 0;
        for (int i = low; i <= high; i++) {
            if(nums[i] == target){
                sum++;
            }
        }
        return sum;
    }
}
class MaxScoreIndicesImpl2 implements MaxScoreIndices{

    @Override
    public List<Integer> maxScoreIndices(int[] nums) {
        int leftSum = 0;
        int rightSum = 0;
        int[] dp = new int[1 + nums.length];
        rightSum = validNumber(nums,0,nums.length - 1, 1);
        dp[0] = rightSum;
        int max = dp[0];
        for (int i = 1; i <= nums.length; i++) {
            if(nums[i - 1] == 0){
                leftSum++;
            }else{
                rightSum--;
            }
            dp[i] = leftSum + rightSum;
            if(dp[i] > max){
                max = dp[i];
            }
        }
        List<Integer> ans = new LinkedList<>();
        for (int i = 0; i < dp.length; i++) {
            if(max == dp[i]) {
                ans.add(i);
            }
        }
        return ans;
    }
    private int validNumber(int[] nums,int low,int high,int target){
        int sum = 0;
        for (int i = low; i <= high; i++) {
            if(nums[i] == target){
                sum++;
            }
        }
        return sum;
    }
}